<?php $authUserId = $session->read('Auth.User.id');?>
<?php 
	$alreadySubscribed = 0;
	if($authUserId)
	{
		//cached result for logged in user.
		$cookieName = 'newsletter_subscribed_'.$authUserId;
		if(!isset($_COOKIE[$cookieName]))
		{
			$alreadySubscribed = cri('Newsletters.Subscriber')->find('count',array('conditions'=>array('Subscriber.entity_id'=>$authUserId)));
			setcookie($cookieName,$alreadySubscribed,0,'/');
		}else
		{
			$alreadySubscribed = $_COOKIE[$cookieName];
		}
	}	
?>
<?php
 if($alreadySubscribed < 1 || empty($authUserId))
{?>
	
	<div class="">
		<?php
		if($session->read('Auth.model')==null)
		{?>
			<?php echo $form->create('Subscriber',array('url'=>'/newsletters/subscribers/subscribe','id'=>'news','class'=>'login_box'))?>
			<?php echo $form->input('name',array('label'=>'','placeholder'=>__('Name'),'id'=>'name','class'=>' textbox_1'));?>
			<?php echo $form->input('email',array('label'=>'','placeholder'=>__('Email'),'id'=>'email','class'=>' textbox_1'));?>
			<?php echo $form->end(array('id'=>'newsletter_submit','class'=>'go_btn','label'=>__('GO',true),));?>
		
			<script type="text/javascript">
			$(document).ready(function()
			{
				$('#newsletter_submit').click(function()
				{
					var name = $('#name').val();
					var email = $('#email').val();
					var status = isValidEmail(email,true);
					if(name=='' || name=='Name')
					{
						message = "<?php echo __('Enter correct name')?>";
						alert(message);
						return false;
					}
			
					if(status == false || email=='Email')
					{
						message = "<?php echo __('Enter valid email id')?>";
						alert(message);
						return false;
					}
					
					
			
				})
				
			});
			function isValidEmail(email, required) {
			    if (required==undefined) {   // if not specified, assume it's required
			        required=true;
			    }
			    if (email==null) {
			        if (required) {
			            return false;
			        }
			        return true;
			    }
			    if (email.length==0) {  
			        if (required) {
			            return false;
			        }
			        return true;
			    }
			    if (! allValidChars(email)) {  // check to make sure all characters are valid
			        return false;
			    }
			    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
			        return false;
			    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
			        return false;
			    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
			        return false;
			    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
				return false;
			    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
				return false;
			    }
			    return true;
			}
			
			function allValidChars(email) {
			  var parsed = true;
			  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
			  for (var i=0; i < email.length; i++) {
			    var letter = email.charAt(i).toLowerCase();
			    if (validchars.indexOf(letter) != -1)
			      continue;
			    parsed = false;
			    break;
			  }
			  return parsed;
			}
			</script>
	<?php }else{?>
			<?php echo __('If you would like to receive latest info, Please subscribe to our newsletter',true)?><br/><br/>
			<?php echo $html->link(__('Subscribe',true),array('controller'=>"subscribers",'action'=>"subscribe",'plugin'=>'newsletters'),array('class'=>'button','onclick'=>"deleteNwsCookie();return true;"))?>
			<script type="text/javascript">
				function deleteNwsCookie()
				{
					<?php setcookie($cookieName,"",0,'/');?>
					
					//cookieName = "<?php //echo $cookieName?>";
					//var expdate = new Date();
					//expdate.setTime(expdate.getTime() - 1);
					//document.cookie = cookieName += "=; expires=" + expdate.toGMTString() + ";";
				}
			</script>
		<?php }?>
		<div class="clear"></div>
	</div>
<?php }else{
return false;
}?>